package com.web.dao.impl;

import com.web.dao.StudentDAO;
import com.web.entity.Student;
import com.web.util.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDAOImpl  implements StudentDAO {
    @Override
    public List<Student> selectAllStudents() {
        return JDBCUtils.query(Student.class,"select * from student");
    }

    @Override
    public int selectStudentsCount() {
        return JDBCUtils.queryCount("select count(*) from student");
    }

    @Override
    public List<Student> selectStudentsPage(int start, int len) {
        return JDBCUtils.query(Student.class,"select * from student limit ?,?",start,len);
    }

    @Override
    public Student selectStudentById(int id) {
        List<Student> students = JDBCUtils.query(Student.class, "select * from student where id =?", id);
        if(students != null && students.size() > 0){
            return students.get(0);
        }
        return null;
    }

    @Override
    public void insertStudent(Student student) {
        JDBCUtils.update("insert into student(name,age,gender,cellphone,address) values(?,?,?,?,?) ",
                student.getName(),student.getAge(),student.getGender(),student.getCellphone(),student.getAddress());
    }

    @Override
    public void updateStudentById(Student student) {
        JDBCUtils.update("update student set name=?,age=?,gender=?,cellphone=?,address=? where id=?",
                student.getName(),student.getAge(),student.getGender(),student.getCellphone(),student.getAddress(),student.getId());
    }

    @Override
    public void deleteStudentById(int id) {
        JDBCUtils.update("delete from student where id =?",id);
    }


}
